<template>
  <div class="app-container">
    <!--工具栏-->
    <!--    <div class="head-container">-->
    <!--表单组件-->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="520px"
    >
      <el-form ref="form" inline :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model.trim="form.name" style="width: 380px;" />
        </el-form-item>
        <el-form-item label="发布者">
          <el-input v-if="false" v-show="false" v-model="form.userId" />
          <el-input v-model="form.createrName" style="width: 150px;" readonly="readonly" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.type" filterable placeholder="请选择" style="width: 135px;">
            <el-option v-for="item in dict.association_type" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-permission="permission.verify" label="审核状态">
          <el-select v-model="form.verifyStatus" filterable placeholder="请选择" style="width: 150px;">
            <el-option v-for="item in dict.info_verify_status" :key="item.id" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio v-for="item in dict.data_status" :key="item.id" v-model="form.isEnable" :label="item.value">
            {{ item.label }}
          </el-radio>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="form.brief" :rows="3" type="textarea" style="width: 380px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="11" :lg="11" :xl="11" style="margin-bottom: 10px">
        <el-card class="box-card" shadow="never">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <label class="el-form-item-label">名称</label>
            <el-input
              v-model="query.name"
              clearable
              placeholder="名称"
              style="width: 205px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
            <label class="el-form-item-label" style="width: 20px">类型</label>
            <el-select
              v-model="query.type"
              clearable
              size="small"
              placeholder="类型"
              class="filter-item"
              style="width: 80px"
              @change="crud.toQuery"
            >
              <el-option v-for="item in dict.association_type" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
            <rrOperation :crud="crud" />
          </div>
          <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
          <crudOperation :permission="permission" />
          <!--表格渲染-->
          <el-table
            ref="table"
            v-loading="crud.loading"
            :data="crud.data"
            size="small"
            highlight-current-row
            style="width: 100%;"
            @selection-change="crud.selectionChangeHandler"
            @current-change="handleCurrentChange"
          >
            <el-table-column type="selection" width="45px" />
            <el-table-column prop="name" label="名称" min-width="170" />
            <el-table-column prop="avatarUrl" label="会标" min-width="60">
              <template slot-scope="scope">
                <el-image
                  :src=" baseApi + '/upload/图片/' + scope.row.avatarUrl"
                  :preview-src-list="[baseApi + '/upload/图片/' + scope.row.avatarUrl]"
                  fit="contain"
                  lazy
                  class="el-avatar"
                >
                  <div slot="error">
                    <i class="el-icon-document" />
                  </div>
                </el-image>
              </template>
            </el-table-column>
            <el-table-column prop="type" label="类型" width="50px">
              <template slot-scope="scope">
                {{ dict.label.association_type[scope.row.type] }}
              </template>
            </el-table-column>
            <el-table-column prop="verifyStatus" label="审核状态" width="80px">
              <template slot-scope="scope">
                <div v-if="scope.row.verifyStatus=='0'">{{ dict.label.info_verify_status[scope.row.verifyStatus] }}
                </div>
                <div v-if="scope.row.verifyStatus=='1'" style="color: limegreen">{{ dict.label.info_verify_status[scope.row.verifyStatus] }}
                </div>
                <div v-if="scope.row.verifyStatus=='2'" style="color: #FF4949">{{ dict.label.info_verify_status[scope.row.verifyStatus] }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="isEnable" label="状态" width="60px">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.isEnable" active-color="#409EFF" inactive-color="#F56C6C" @change="changeEnabled(scope.row, scope.row.isEnable)" />
              </template>
            </el-table-column>
            <!-- <el-table-column
              v-permission="['admin','association:edit','association:del']"
              label="操作"
              width="115px"
              align="center"
              fixed="right"
            >
              <template slot-scope="scope">
                <udOperation
                  :data="scope.row"
                  :permission="permission"
                />
              </template>
            </el-table-column> -->
          </el-table>
          <!--分页组件-->
          <pagination />
        </el-card>
      </el-col>
      <!-- 校友会详情 -->
      <el-col :xs="24" :sm="24" :md="13" :lg="13" :xl="13">
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <span>校友会详情</span>
          </div>
          <AssociationUserInfo ref="AssociationUserInfo" :permission="permission" />
        </el-card>
      </el-col>
    </el-row>
    <!--    </div>-->
  </div>
</template>

<script>
import {
  mapGetters
} from 'vuex'
import crudAssociation from '@/api/anniversary/association'
import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import AssociationUserInfo from '@/views/anniversary/association/associationUserInfo'

const defaultForm = {
  id: null,
  name: null,
  userId: null,
  type: null,
  brief: null,
  verifyStatus: '0',
  creater: null,
  createTime: null,
  reviewer: null,
  reviewTime: null,
  sort: 999,
  remark: null,
  isEnable: 'false'
}
export default {
  name: 'Association',
  components: {
    pagination,
    crudOperation,
    rrOperation,
    AssociationUserInfo
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['association_type', 'info_verify_status', 'data_status'],
  cruds() {
    return CRUD({
      title: '校友会',
      url: 'api/association',
      sort: 'id,desc',
      crudMethod: { ...crudAssociation
      }
    })
  },
  data: function() {
    return {
      permission: {
        add: ['admin', 'association:add'],
        edit: ['admin', 'association:edit'],
        del: ['admin', 'association:del'],
        verify: ['admin', 'association:verify']
      },
      rules: {
        name: [{
          required: true,
          message: '名称不能为空',
          trigger: 'blur'
        }]
      },
      queryTypeOptions: [{
        key: 'name',
        display_name: '名称'
      },
      {
        key: 'type',
        display_name: '类型'
      },
      {
        key: 'brief',
        display_name: '简介'
      },
      {
        key: 'isEnable',
        display_name: '状态'
      }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'baseApi'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.id == null) {
        form.userId = this.user.id
        form.createrName = this.user.username
      } else {
        form.userId = `${form.creater.id}`
        form.createrName = `${form.creater.username}`
      }
      if (form.type != null) {
        form.type = `${form.type}`
      }
      form.userType = `${form.userType}`
      form.verifyStatus = `${form.verifyStatus}`
      form.isEnable = `${form.isEnable}`
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.data_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudAssociation.edit(data).then(res => {
          this.crud.notify(this.dict.label.data_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.isEnable = !data.isEnable
        })
      }).catch(() => {
        data.isEnable = !data.isEnable
      })
    },
    // 选中校友会后，设置校友会人员详情数据与校友会基础信息
    handleCurrentChange(val) {
      // 设置基础信息
      if (val) {
        this.$refs.AssociationUserInfo.temp = val
        this.$refs.AssociationUserInfo.associationId = val.id
        this.$refs.AssociationUserInfo.query.associationId = val.id
        this.$refs.AssociationUserInfo.crud.toQuery()
      }
    }
  }
}
</script>

<style scoped>

</style>
